package com.li.controller;


import com.li.model.UserProject;
import com.li.service.UserProjectService;
import io.swagger.annotations.*;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
@Api(tags = "用户工程管理")
@RestController
public class UserProjectController {
    @Autowired
    private UserProjectService userProjectService;


    @ApiOperation(value = "通过用户id获取的所有工程", nickname = "queryAllProject", notes = "通过用户id获取的所有工程", response = UserProject.class, responseContainer = "List")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "success", response = UserProject.class, responseContainer = "List")
                        })
    @GetMapping(value = "/com/userPro/query/{uid}", produces = { "application/json" })
    public ResponseEntity<List<UserProject>> queryAllProject(@PathVariable long uid){
            List<UserProject> userProjects = this.userProjectService.queryAllByUid(uid);
            if (CollectionUtils.isEmpty(userProjects)){
                return ResponseEntity.ok(Collections.emptyList());
            }
        return ResponseEntity.ok(userProjects);
    }

    @ApiOperation(value = "更新某个工程", nickname = "updateProject", notes = "更新某个工程")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "success")
    })
    @PostMapping(value = "/com/userPro/update/{id}", produces = { "application/json" })
    public ResponseEntity<Void> updateProject( @PathVariable("id") long id,  @RequestBody(required = false)UserProject userProject){
        Optional<UserProject> dtoOptional = userProjectService.queryProjectById(id);
        if (!dtoOptional.isPresent()){
            ResponseEntity.notFound().build();
        }
        userProjectService.updateProject(userProject);
        return ResponseEntity.ok().build();
    }


    @ApiOperation(value = "删除某个工程", nickname = "deletePro", notes = "删除某个工程")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "success")
    })
    @GetMapping(value = "/com/userPro/delete/{id}", produces = { "application/json" })
    public ResponseEntity<Void> deletePro(@PathVariable("id") long id){
        userProjectService.deleteProject(id);
        return ResponseEntity.ok().build();
    }


    @ApiOperation(value = "添加工程", nickname = "addProject", notes = "添加工程" ,response = UserProject.class)
    @ApiResponses(value = {
            @ApiResponse(code = 201, message = "created", response = UserProject.class)
    })
    @PutMapping(value = "/com/userPro/add")
    public ResponseEntity<UserProject>addProject(@RequestBody(required = false)UserProject userProject){
        this.userProjectService.addProject(userProject);
        return ResponseEntity.ok(userProject);
    }
}
